package com.sun.synchronize;

public class SaleTick {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        //创建三个线程来执行
        new Thread(() -> {
            for (int i = 0; i < 60; i++) {
                ticket.sale();
            }
        }, "AA").start();
        new Thread(() -> {
            for (int i = 0; i < 60; i++) {
                ticket.sale();
            }
        }, "BB").start();
        new Thread(() -> {
            for (int i = 0; i < 60; i++) {
                ticket.sale();
            }
        }, "CC").start();
    }
}


//资源类
class Ticket {
    int num = 50;

    //操作方法
    public synchronized void sale() {
        if (num > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出了：" + num-- + "剩下:" + num);
        }
    }
}
